{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "import matchzoo as mz\n",
    "from matchzoo import DataGenerator\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import keras\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import classification_report\n",
    "from sklearn.metrics import f1_score\n",
    "import scipy\n",
    "\n",
    "from sklearn.metrics import precision_recall_fscore_support"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data and Preprocess It"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Processing text_left with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit: 100%|█| 716/716 [00:00<00:00, 9070.57it/s]\n",
      "Processing text_right with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit: 100%|█| 1025/1025 [00:00<00:00, 3849.38it/s]\n",
      "Processing text_left with extend: 100%|██████████████████████████████████████████| 716/716 [00:00<00:00, 179483.72it/s]\n",
      "Processing text_right with extend: 100%|███████████████████████████████████████| 1025/1025 [00:00<00:00, 256743.00it/s]\n",
      "Building VocabularyUnit from a datapack.: 100%|███████████████████████████| 112803/112803 [00:00<00:00, 3133436.26it/s]\n",
      "Processing text_left with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit => WordHashingUnit: 100%|█| 716/716 [00:00<00:00, 6404.19it/s]\n",
      "Processing text_right with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit => WordHashingUnit: 100%|█| 1025/1025 [00:00<00:00, 2840.78it/s]\n",
      "Processing text_left with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit => WordHashingUnit: 100%|█| 200/200 [00:00<00:00, 5550.99it/s]\n",
      "Processing text_right with chain_transform of TokenizeUnit => LowercaseUnit => PuncRemovalUnit => StopRemovalUnit => NgramLetterUnit => WordHashingUnit: 100%|█| 734/734 [00:00<00:00, 2876.29it/s]\n"
     ]
    }
   ],
   "source": [
    "train_raw = mz.pack(pd.read_csv('toy_matchzoo_train.csv', index_col=0))\n",
    "test_raw = mz.pack(pd.read_csv('toy_matchzoo_test.csv', index_col=0))\n",
    "\n",
    "train_raw.relation['label'] = train_raw.relation['label'].astype('float32')\n",
    "test_raw.relation['label'] = test_raw.relation['label'].astype('float32')\n",
    "\n",
    "preprocessor = mz.preprocessors.DSSMPreprocessor()\n",
    "\n",
    "preprocessor.fit(train_raw)\n",
    "preprocessor.context\n",
    "\n",
    "train_preprocessed = preprocessor.transform(train_raw)\n",
    "test_preprocessed = preprocessor.transform(test_raw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter \"name\" set to DSSM.\n",
      "Parameter \"task\" set to Ranking Task.\n"
     ]
    }
   ],
   "source": [
    "model = mz.models.DSSM()\n",
    "model.params['input_shapes'] = preprocessor.context['input_shapes']\n",
    "model.params['mlp_num_layers'] = 3\n",
    "model.params['mlp_num_units'] = 300\n",
    "model.params['mlp_num_fan_out'] = 128\n",
    "model.params['mlp_activation_func'] = 'relu'\n",
    "model.guess_and_fill_missing_params()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since no loss function set, default is Mean Square Error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mse\n"
     ]
    }
   ],
   "source": [
    "model.build()\n",
    "model.compile()\n",
    "print(model._params['task'].loss)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Train Model\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 4986 samples, validate on 1248 samples\n",
      "Epoch 1/20\n",
      " - 4s - loss: 0.1791 - val_loss: 0.1581\n",
      "Epoch 2/20\n",
      " - 4s - loss: 0.1545 - val_loss: 0.1510\n",
      "Epoch 3/20\n",
      " - 4s - loss: 0.1484 - val_loss: 0.1461\n",
      "Epoch 4/20\n",
      " - 4s - loss: 0.1444 - val_loss: 0.1428\n",
      "Epoch 5/20\n",
      " - 4s - loss: 0.1418 - val_loss: 0.1409\n",
      "Epoch 6/20\n",
      " - 4s - loss: 0.1403 - val_loss: 0.1399\n",
      "Epoch 7/20\n",
      " - 4s - loss: 0.1396 - val_loss: 0.1393\n",
      "Epoch 8/20\n",
      " - 4s - loss: 0.1392 - val_loss: 0.1391\n",
      "Epoch 9/20\n",
      " - 4s - loss: 0.1390 - val_loss: 0.1390\n",
      "Epoch 10/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 11/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 12/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 13/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 14/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 15/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 16/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 17/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 18/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 19/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n",
      "Epoch 20/20\n",
      " - 4s - loss: 0.1389 - val_loss: 0.1389\n"
     ]
    }
   ],
   "source": [
    "x_train, y_train = train_preprocessed.unpack()\n",
    "x_test, y_test = test_preprocessed.unpack()\n",
    "\n",
    "history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, verbose=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Evaluate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]\n",
      " [0.16652368]]\n",
      "[[1.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]\n",
      " [1.]\n",
      " [0.]\n",
      " [0.]\n",
      " [0.]]\n"
     ]
    }
   ],
   "source": [
    "y_pred = model.predict(x_test)\n",
    "\n",
    "print(y_pred[0:10])\n",
    "print(y_test[0:10])\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\patelr9\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\sklearn\\metrics\\classification.py:1135: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 due to no predicted samples.\n",
      "  'precision', 'predicted', average, warn_for)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "0.0\n",
      "0.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\patelr9\\AppData\\Local\\Continuum\\anaconda3\\lib\\site-packages\\sklearn\\metrics\\classification.py:1135: UndefinedMetricWarning: F-score is ill-defined and being set to 0.0 due to no predicted samples.\n",
      "  'precision', 'predicted', average, warn_for)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "#classify\n",
    "y_pred_cutoff = y_pred > .5\n",
    "\n",
    "prec, recall, f1beta, support = precision_recall_fscore_support(y_test, y_pred_cutoff, pos_label=1, average='binary')\n",
    "\n",
    "print(prec)\n",
    "print(recall)\n",
    "print(f1beta)\n",
    "print(f1_score(y_test, y_pred_cutoff, pos_label=1, average='binary'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
